import asyncio

from com.arcfox.base.base_spider import BaseSpider, ExecuteType
from com.arcfox.middleware.async_redis_middleware import open_redis
from com.arcfox.school.processor.ip33.ip33_area_processor import AreaProcessor
from com.arcfox.util import async_request as requests


class AreaSpider(BaseSpider):
    def __init__(self):
        super().__init__()
        self.url = "http://www.ip33.com/area/2019.html"
        self.processor = AreaProcessor()

    @open_redis
    async def init_data_version(self, client):
        pass

    async def _pull_task(self):
        return None, ExecuteType.ONCE

    async def _crawl_by_task(self, task):
        resp = await requests.get_with_session(self.url, headers=self.get_default_header(), timeout=5)
        if resp.code == 200:
            await self.processor.parse_and_save_data(resp.response)


if __name__ == '__main__':
    asyncio.run(AreaSpider().start_crawl())
